//
//  STTimerSelView.m
//  YiYueHairdresser
//
//  Created by zhouzhouzhou on 2018/10/9.
//  Copyright © 2018年 YuYiLian. All rights reserved.
//

#import "STTimerSelView.h"
#import "STTimerDayCell.h"

@implementation STTimerSelView

-(id)init{
    self = [super init];
   
    return self;
}

-(void)setTimerSelectWithData:(id)data{
    if(!self.selNums.count){
        self.selNums = [NSMutableArray arrayWithCapacity:0];
    }
        
    tiemrSels = data;
    
//    [self loadBackView];
//    [self loadCollectionView];
    
    [self loadBackView];
    [self loadCollectionView];
    
//    [timerCollection reloadData];
}

-(void)loadBackView{
    if (!backView) {
        backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        backView.backgroundColor = [UIColor blackColor];
        backView.alpha = 0.3;
        UITapGestureRecognizer *tapGesture1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionCancelSelTimer)];
        [backView addGestureRecognizer:tapGesture1];
         [self addSubview:backView];
        
        if (self.titleStr.length) {
            float viewW =SCREEN_WIDTH;
            float w = (viewW-81) / 4;
            float viewH = (w*0.4+20)*3+20;
            UILabel * labTitle = [[UILabel alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-viewH-50-44, SCREEN_WIDTH, 44)];
            [self addSubview:labTitle];
            labTitle.backgroundColor = [UIColor whiteColor];
            labTitle.textAlignment = NSTextAlignmentCenter;
            labTitle.text = self.titleStr;
            labTitle.font = FONT(15);
        }
    }
}

-(void)actionCancelSelTimer{
    [self removeFromSuperview];
}

-(void)loadCollectionView{
    float viewW =SCREEN_WIDTH;
    float w = (viewW-81) / 4;
    float h = w*0.4;
    
    float viewH = (w*0.4+20)*3+20;
//    float viewY =SCREEN_HEIGHT-viewH;
    
    UICollectionViewFlowLayout * flow =[[UICollectionViewFlowLayout alloc]init];
    flow.itemSize=CGSizeMake(w, h);
    flow.headerReferenceSize = CGSizeMake(SCREEN_WIDTH,15.0f);
    flow.sectionInset=UIEdgeInsetsMake(0,10,0,10);
    timerCollection=[[UICollectionView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-viewH-50, viewW, viewH) collectionViewLayout:flow];
    [timerCollection registerClass:[STTimerDayCell class] forCellWithReuseIdentifier:@"STTimerDayCell"];
    timerCollection.delegate = self;
    timerCollection.dataSource = self;
    //    timerCollection.scrollEnabled=NO;
    timerCollection.showsHorizontalScrollIndicator = NO;
    timerCollection.showsVerticalScrollIndicator = NO;
    timerCollection.backgroundColor = [UIColor whiteColor];
    [self addSubview:timerCollection];
    
    UIButton * sureBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-50, SCREEN_WIDTH, 50)];
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    sureBtn.titleLabel.font = FONT(17);
    sureBtn.backgroundColor = ButtonBackColor;
    [sureBtn addTarget:self action:@selector(actionSureSelTimer) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:sureBtn];
    
}


#pragma mark -collectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return tiemrSels.count;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 20;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"STTimerDayCell";
    STTimerDayCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    cell.isSel = NO;
    [cell setOwnCollectionCellWithData:tiemrSels[indexPath.row]];
//
    if(self.selNums.count){
        if([self.selNums containsObject:@(indexPath.row)]){
            [cell actionChangeDayBtn];
        }else{
            cell.isSel = NO;
        }
    }
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if([self.unAbles containsObject:@(indexPath.row)]){
        return;
    }
    STTimerDayCell * cell = (STTimerDayCell *)[collectionView cellForItemAtIndexPath:indexPath];
    [cell actionChangeDayBtn];
    
    if (self.isMulti) {
        if ([self.selNums containsObject:@(indexPath.row)]) {
            [self.selNums removeObject:@(indexPath.row)];
        }else{
            [self.selNums addObject:@(indexPath.row)];
        }
    }else{
        [self.selNums insertObject:@(indexPath.row) atIndex:0];
    }
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (!self.isMulti) {
        STTimerDayCell * cell = (STTimerDayCell *)[collectionView cellForItemAtIndexPath:indexPath];
        [cell actionChangeDayBtn];
    }  
}

-(void)actionSureSelTimer{
    [self actionCancelSelTimer];
    if(!self.selNums.count){
        return;
    }
    if ([self.delegate respondsToSelector:@selector(timerSelView:SeltectNum:)]) {
        [self.delegate timerSelView:self SeltectNum:self.selNums];
    }
}

@end
